Laptop As Code

A stateless computer

Summary

  • Laptop As Code?

  • Bash

  • Ansible

  • Laptop As Code with Ansible

  • Demo

How am I?

Mehdi Rebiai

Laptop As Code?

code

Once upon a time…​

You have a new laptop

🎉 💻 🥳 🥰

…​ and then…​

  • Some installations…​ 🧰 ⚒️

  • Some customizations…​ 🔧 🦄

  • And sometimes…​ 🥵

…​ and one day 🔥 !

computer on fire

Or lost, stolen, broken, …​

…​ finally

Solution 🪄 ⇒ Documentation about installation

the end

…​ but

  • My personal documents / configurations ?

  • Is the documentation up-to-date ? 🤣

  • Any fresh installation (new incomer/computer) = documentation + 🥵 ?

Another point of view (1/2)

Your laptop deserves the same attention as your production code !

Another point of view (2/2)

evolution

A computer is stateless !

  • (Almost) Everything as Code in SCM

git logo

  • Otherwise ⇒ Cloud storage

onedrive logo google docs

And my passwords 😱 ?

  • Keepass file in SCM

  • Lastpass, 1Password, …​

My new challenge : rebuild my 💻 in 30 minutes !

challenge
OS already installed

Bash

press any key

run.sh bash logo

install() {
  installCommon
  installSDK
  installGit
  installDocker
  installKubernetes
  ...
  # TODO To be continued
}
installCommon() {
  ...
  sudo apt -y install git graphviz gimp curl git-gui aspell-fr openssh-server filezilla nodejs npm wget pdfshuffler tree gtk-recordmydesktop terminator ethtool jq snapdi tig kakoune ranger httpie ffmpeg keepassxc
  # TODO To be continued
}
...

A good scripting?

notbad
But
  • Many SCM forks & customization

  • Maintainability ?

New challenge(s)

  • Shareable scripting

  • Easy customization

  • Readable configuration

  • Do you know Ansible?

Ansible

magic

Ansible (1/8)

Ansible automates the management of remote systems and controls their desired state

Ansible (2/8)

ansible basic
  • Ansible on Control node

  • Python on Managed Nodes

  • Inventory YAML or INI

  • SSH connections

  • YAML task description

  • Multi-OS

Ansible (3/8)

Inventory
[webservers]
node1.example.com
node2.example.com

[dbservers]
node3.example.com

Ansible (4/8)

ansible-playbook playbook.yml
---
- name: Update web servers
  hosts: webservers
  remote_user: root
  tasks:
  - name: Ensure apache is at the latest version
    tags: httpd
    ansible.builtin.yum:
      name: httpd
      state: latest
  - name: Write the apache config file
    tags: httpd
    ansible.builtin.template:
      src: /srv/httpd.j2
      dest: /etc/httpd.conf

- name: Update db servers
  hosts: dbservers
  remote_user: root
  tasks:
  - name: Ensure postgresql is at the latest version
    tags: pg
    ansible.builtin.yum:
      name: postgresql
      state: latest
  - name: Ensure that postgresql is started
    tags: pg
    ansible.builtin.service:
      name: postgresql
      state: started

Ansible (5/8)

  • A Task calls a Module (yum, apt, service…​) ⇒ Built-In index

  • Every Task can contain Tag(s)

  • Some Tasks can be defined in Roles

  • A Playbook can call Tasks and/or Roles

Ansible (6/8)

Ansible Galaxy
The Hub is not mandatory!

Ansible (7/8)

Role
tasks/
  main.yml     #  <-- tasks file can include smaller files if warranted
handlers/      #
  main.yml     #  <-- handlers file
templates/     #  <-- files for use with the template resource
  ntp.conf.j2  #  <-- templates end in .j2
files/         #
  bar.txt      #  <-- files for use with the copy resource
  foo.sh       #  <-- script files for use with the script resource
vars/          #
  main.yml     #  <-- variables associated with this role
defaults/      #
  main.yml     #  <-- default lower priority variables for this role
meta/          #
  main.yml     #  <-- role dependencies

Ansible (8/8)

Laptop As Code with Ansible
  • 127.0.0.1 in Inventory

  • Ansible local connection

  • ansible-pull -U <giturl> …​ =

    • git clone/pull …​

    • ansible-playbook …​

Sources - LearnLinuxTV